/*
 * Bombs.cpp
 *
 *  Created on: 2010-05-27
 *      Author: mateusz
 */

#include "Bombs.h"

#include <time.h>

using namespace std;

//------------------------------------------------------------------------------

Bombs::Bombs() {
	delay = 2;
	numberOfBombs = 3;
}

//------------------------------------------------------------------------------

Bombs::~Bombs() {
}

//------------------------------------------------------------------------------

bool Bombs::addBomb( int x, int y, Map* map, bool mine ) {
	int numberOfMineBombs = 0;
	for( unsigned i = 0; i < bombs.size(); i++ ) {
		if( bombs[i].isMine() ) {
			numberOfMineBombs++;
		}
	}

	if( !mine || numberOfMineBombs < numberOfBombs ) {
		Bomb bomb(x,y,time(NULL),mine);

		map->map[x/20*map->size+y/20] = 3;

		bombs.push_back(bomb);

		return true;
	}

	return false;
}

//------------------------------------------------------------------------------

void Bombs::check(Map* map) {
	time_t now = time(NULL);

	for( unsigned i = 0; i < bombs.size(); i++ ) {
		if( now - bombs[i].getSetTime() > delay  || map->map[bombs[i].getX()/20*map->size+bombs[i].getY()/20] < 3 ) {
			bombs[i].blow(map);
			bombs.erase(bombs.begin()+i);
			i--;
		}
	}
}

//------------------------------------------------------------------------------

void Bombs::clear() {
	bombs.clear();
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
